package com.raspberry.envmonitor;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.widget.Toast;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.DefaultHandler;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private Context mContext = MainActivity.this;
    private long exitTimes;
    private SoundPool soundpool;
    private Map<Integer, Integer> soundMap = new HashMap<Integer, Integer>();
    private BridgeWebView webView;
    private static String indexURL = "file:///android_asset/index.html";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.getSupportActionBar().hide();
        webView = findViewById(R.id.webView);
        WebSettings settings = webView.getSettings();
        settings.setDomStorageEnabled(true);
        webView.setDefaultHandler(new DefaultHandler());
        webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
        webView.setBackgroundColor(Color.rgb(255, 255, 255));
        webView.loadUrl(indexURL);

        if (Build.VERSION.SDK_INT > 21) {
            SoundPool.Builder builder = new SoundPool.Builder();
            builder.setMaxStreams(1);
            AudioAttributes.Builder attrBuilder = new AudioAttributes.Builder();
            attrBuilder.setLegacyStreamType(AudioManager.STREAM_SYSTEM);
            builder.setAudioAttributes(attrBuilder.build());
            soundpool = builder.build();
        } else {
            soundpool = new SoundPool(1, AudioManager.STREAM_SYSTEM, 0);
        }
        soundMap.put(1, soundpool.load(this, R.raw.beep, 1));

        //注册播放声音的JSBridge
        webView.registerHandler("playAlert", (data, function) -> {
            soundpool.play(1,1,1,0,0,1);
            function.onCallBack("{\"code\":0}");
        });
    }


    //返回监听
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTimes) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTimes = System.currentTimeMillis();
            } else {
                Intent homeIntent = new Intent(Intent.ACTION_MAIN);
                homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                homeIntent.addCategory(Intent.CATEGORY_HOME);
                startActivity(homeIntent);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        //清理内存
        if (soundpool != null) {
            soundpool.release();
        }
        if (webView != null) {
            webView.removeAllViews();
            webView.destroy();
        }
        super.onDestroy();
    }
}
